package cn.iocoder.yudao.module.mqtt.framework.handler;

import cn.iocoder.yudao.module.mqtt.service.mqtt.MqttMessageService;
import cn.iocoder.yudao.module.mqtt.service.subscribewater.SubscribeWaterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException;

/**
 * 实例特定的MQTT消息处理器
 * 每个MQTT实例都有自己的处理器，直接包含配置信息
 *
 * @author 芋道源码
 */
@Slf4j
public class InstanceSpecificMessageHandler implements MessageHandler {

    private final Long configId;
    private final String clientId;
    private final MqttMessageService mqttMessageService;

    private final SubscribeWaterService subscribeWaterService;

    public InstanceSpecificMessageHandler(Long configId, String clientId, MqttMessageService mqttMessageService, SubscribeWaterService subscribeWaterService) {
        this.configId = configId;
        this.clientId = clientId;
        this.mqttMessageService = mqttMessageService;
        this.subscribeWaterService = subscribeWaterService;
    }

    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        try {
            String payload = message.getPayload().toString();
            MessageHeaders headers = message.getHeaders();
            
            // 获取主题信息
            String topicName = headers.get("mqtt_receivedTopic") != null ? 
                    headers.get("mqtt_receivedTopic").toString() : "unknown";
            
            // 获取消息ID
            String messageId = headers.get("mqtt_id") != null ? 
                    headers.get("mqtt_id").toString() : null;
            
            // 获取QoS
            Integer qos = headers.get("mqtt_qos") != null ? 
                    Integer.valueOf(headers.get("mqtt_qos").toString()) : 0;
            
            // 获取retained标志
            Boolean retained = headers.get("mqtt_retained") != null ? 
                    Boolean.valueOf(headers.get("mqtt_retained").toString()) : false;
            
            log.info("收到MQTT消息 - 配置ID: {}, 客户端ID: {}, 主题: {}, 内容: {}", 
                    configId, clientId, topicName, payload);
            
            // 保存消息到数据库 生产 关闭  ，后面做到配置项目
            mqttMessageService.saveReceivedMessage(configId, topicName, payload, qos, retained, clientId, messageId);


            // 根据主题进行业务处理 - 异步
            processMessageByTopic(configId, topicName, payload, qos, retained, clientId, messageId);
            
        } catch (Exception e) {
            log.error("处理MQTT消息时发生错误，配置ID: {}", configId, e);
        }
    }

    /**
     * 根据主题进行业务处理
     */
    private void processMessageByTopic(Long mqttConfigId,  String topicName, String payload,Integer qos,
                                       Boolean retained, String clientId,String messageId) {
        // 这里可以根据不同的主题进行不同的业务处理
        switch (topicName) {
            case "2025-24": //测试

                subscribeWaterService.createSubscribeWater(configId, topicName, payload, qos, retained, clientId, messageId);
                break;
            case "mqtt":   //线上
//                processLampStatusMessage(mqttConfigId, payload);
                log.info("已知主题: {}, 内容: {}", topicName, payload);
                break;
            default:
                log.info("未识别的主题: {}, 内容: {}", topicName, payload);
                break;
        }
    }

    public Long getConfigId() {
        return configId;
    }

    public String getClientId() {
        return clientId;
    }
} 